package cn.stylefeng.guns.sys.modular.system.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.stylefeng.guns.base.auth.context.LoginContextHolder;
import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.sys.core.exception.enums.BizExceptionEnum;
import cn.stylefeng.guns.sys.core.log.LogObjectHolder;
import cn.stylefeng.guns.sys.modular.system.entity.BtyWlCheliang;
import cn.stylefeng.guns.sys.modular.system.service.BtyWlChelianngService;
import cn.stylefeng.guns.sys.modular.system.warpper.NoticeWrapper;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.util.ToolUtil;
import cn.stylefeng.roses.kernel.model.exception.ServiceException;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.Date;
import java.util.Map;

/**
 * 物流车辆controller
 */
@Controller
@RequestMapping("/btyWlCheliang")
public class BtyWlCheliangController extends BaseController {

    private String PREFIX = "/modular/system/btyWlCheliang/";

    @Resource
    private BtyWlChelianngService btyWlChelianngService;

    /**
     * 跳转到物流车辆页面
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "btyWlCheliang.html";
    }

    /**
     * 获取通知列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(BtyWlCheliang btyWlCheliang) {

        Page<Map<String, Object>> list = btyWlChelianngService.list(btyWlCheliang);
        Page<Map<String, Object>> wrap = new NoticeWrapper(list).wrap();
        return LayuiPageFactory.createPageInfo(wrap);
    }

    /**
     * 跳转到修改列表
     */
    @RequestMapping("/btyWlCheliang_update/{id}")
    public String noticeUpdate(@PathVariable Long id, Model model) {
        BtyWlCheliang btyWlCheliang = this.btyWlChelianngService.getById(id);
        model.addAllAttributes(BeanUtil.beanToMap(btyWlCheliang));
        LogObjectHolder.me().set(btyWlCheliang);
        return PREFIX + "edit.html";
    }

    /**
     * 修改列表
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public Object update(BtyWlCheliang btyWlCheliang) {
        if (ToolUtil.isOneEmpty(btyWlCheliang, btyWlCheliang.getId())) {
            throw new ServiceException(BizExceptionEnum.REQUEST_NULL);
        }

        this.btyWlChelianngService.updateById(btyWlCheliang);
        return SUCCESS_TIP;
    }

    /**
     * 跳转到添加页面
     */
    @RequestMapping(value = "/btyWlCheliang_add")
    public String btyTxl_addct() {
        return PREFIX + "add.html";
    }

    /**
     * 添加数据
     *
     * @param btyWlCheliang
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public Object add(BtyWlCheliang btyWlCheliang) {
        //this.btyWlChelianngService.add(btyWlCheliang);
        btyWlCheliang.setCreateUser(LoginContextHolder.getContext().getUserId());
        btyWlCheliang.setCreateTime(new Date());
        btyWlChelianngService.save(btyWlCheliang);
        return SUCCESS_TIP;
    }
}
